package com.example.practice.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.example.practice.R;
import com.example.practice.activity.ylVideoPlayActivity;
import com.example.practice.bean.VideoBeanYl;
import com.example.practice.utils.DBUtils;
import com.example.practice.utils.SharedpreferencesUtils;

import java.util.List;

public class VideoListAdapterYl extends BaseAdapter {
    private Context ylContext;
    private List<VideoBeanYl> yl_vbl;
    private int yl_selectedPosition = -1;

    public VideoListAdapterYl(Context context) {
        this.ylContext = context;
    }

    public void setSelectedPosition(int position) {
        yl_selectedPosition=position;
    }

//    设置数据更新界面
    public void setData(List<VideoBeanYl> vbl){
        this.yl_vbl = vbl;
        notifyDataSetChanged();
    }

    class ViewHolder {
        public TextView tv_title;
        public ImageView iv_icon;
    }

    @Override
    public int getCount() {
        return yl_vbl==null?0:yl_vbl.size();
    }

    @Override
    public VideoBeanYl getItem(int position) {
        return yl_vbl==null?null:yl_vbl.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder vh;
        if (convertView == null){
            vh = new ViewHolder();
            convertView = LayoutInflater.from(ylContext).inflate(
                    R.layout.yl_video_list_item,null);
            vh.tv_title=convertView.findViewById(R.id.yl_tv_video_title);
            vh.iv_icon=convertView.findViewById(R.id.yl_iv_left_icon);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        final VideoBeanYl bean = getItem(position);
        vh.iv_icon.setImageResource(R.drawable.course_bar_icon);
        vh.tv_title.setTextColor(Color.parseColor("#333333"));
        if(bean!=null){
            vh.tv_title.setText(bean.secondTitle);
//            设置选中效果
            if(yl_selectedPosition==position){
                vh.iv_icon.setImageResource(R.drawable.course_intro_icon);
                vh.tv_title.setTextColor(Color.parseColor("#009958"));
            } else {
                vh.iv_icon.setImageResource(R.drawable.course_bar_icon);
                vh.tv_title.setTextColor(Color.parseColor("#333333"));
            }
        } else {
            Log.i("err", "err");
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(bean==null){
                    return;
                }
                // todo:播放视频
                String videoPath = bean.videoPath;
                if(TextUtils.isEmpty(videoPath)) {
                    Toast.makeText(ylContext,"本地没有此视频,无法播放",Toast.LENGTH_SHORT).show();
                    return;
                } else{
//                    判断用户是否登录,若登录则把此视频添加到数据库
                    if(SharedpreferencesUtils.readLoginStatus(ylContext)) {
                        String userName = SharedpreferencesUtils.readLoginUserName(ylContext);
                        DBUtils db = DBUtils.getInstance(ylContext);
                        db.saveVideoPlayList(bean,userName);
                    }
                    Intent intent=new Intent(ylContext, ylVideoPlayActivity.class);
                    intent.putExtra("videoPath",videoPath);
                    ylContext.startActivity(intent);
                }
            }
        });
        return convertView;
    }


}
